package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.mapper.SchemeConfigMapper;
import com.hamster.crm.mapper.SchemeEmployeeMapper;
import com.hamster.crm.mapper.SchemeMapper;
import com.hamster.crm.mapper.SchemeResultMapper;
import com.hamster.crm.pojo.Scheme;
import com.hamster.crm.pojo.SchemeConfig;
import com.hamster.crm.pojo.SchemeEmployee;
import com.hamster.crm.pojo.SchemeResult;
import com.hamster.crm.service.SchemeService;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.List;

/**
 * 方案执行管理 service
 * @author Tarry
 * @create 2019/12/20 15:08
 */
@Service
public class SchemeServiceImpl implements SchemeService {
    @Autowired
    private SchemeMapper schemeMapper;
    @Autowired
    private SchemeConfigMapper schemeConfigMapper;
    @Autowired
    private SchemeResultMapper schemeResultMapper;
    @Autowired
    private SchemeEmployeeMapper schemeEmployeeMapper;
    /**
     * 执行比例设置
     * 插入一条执行情况比例
     * @param schemeConfig
     */
    @Override
    public void insertSchemeConfig(SchemeConfig schemeConfig) {
        schemeConfig.setDel("0");
        schemeConfig.setInputTime(new Date());
        schemeConfig.setInputUser("功能待完善");
        schemeConfigMapper.insert(schemeConfig);
    }

    /**
     * 执行比例设置
     * 获取执行比例列表
     * @return
     */
    @Override
    public List<SchemeConfig> listSchemeConfig() {
        QueryWrapper<SchemeConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("del",0);  //设置查询条件，删除标记为0
        List<SchemeConfig> configList = schemeConfigMapper.selectList(wrapper);
        return configList;
    }

    /**
     * 根据id删除方案执行情况
     * @param ids
     */
    @Transactional
    @Override
    public void deleteSchemeConfig(List<String> ids) {
        for (String id : ids) {
            schemeConfigMapper.deleteTag(id); //修改删除标记为 1
        }
    }

    /**
     * 添加方案分栏
     * 添加一条方案
     * @param scheme
     */
    @Override
    public void insertScheme(Scheme scheme) {
        Date date = new Date();  //创建时间和插入时间
        scheme.setDel("0");
        scheme.setState("0");  //0新建  1已下发  2已完成
        scheme.setCreateTime(date);
        scheme.setInputTime(date);
        scheme.setInputUser("功能待完善");
        schemeMapper.insert(scheme);
    }

    /**
     * 添加方案分栏
     * 根据id删除方案
     * @param ids
     */
    @Override
    public void deleteScheme(List<String> ids) {
        for (String id : ids) {
            schemeMapper.deleteTag(id); //修改删除标记为 1
        }
    }

    /**
     * 添加方案分栏
     * 根据id下发方案
     * @param ids
     */
    @Override
    public void sendScheme(List<String> ids) {
        for (String id : ids) {
            schemeMapper.setSchemeState(id,"1"); //修改下发状态为 1已下发  【0新建，1下发，2完成】
        }
    }

    /**
     * 添加方案分栏
     * 查询所有方案列表
     * @return
     */
    @Override
    public List<Scheme> listSchemes() {
        QueryWrapper<Scheme> wrapper = new QueryWrapper<>();
        wrapper.eq("del","0");  //查询所有删除标记为0的记录
        List<Scheme> schemeList = schemeMapper.selectList(wrapper);
        return schemeList;
    }

    /**
     * 方案执行结果列表
     * @param title       方案名称
     * @param participant 参与人员
     * @param currentPage 当前页号
     * @param pageSize     每页条数
     * @return
     */
    @Override
    public PageResult<SchemeEmployee> listSchemeResults(String title, String participant, Integer currentPage, Integer pageSize) {
        //分页
        Page<SchemeEmployee> page = new Page<>(currentPage, pageSize);
        QueryWrapper<SchemeEmployee> wrapper = new QueryWrapper<>();
        //根据任务名称过滤
        if (StringUtils.isNotBlank(title)){
            wrapper.like("scheme_title","%"+title+"%");
        }
        //根据参与人员名过滤
        if (StringUtils.isNotBlank(participant)){
            wrapper.like("employee_name","%"+participant+"%");
        }

        IPage pageList = schemeEmployeeMapper.selectPage(page, wrapper);
        List<SchemeEmployee> currentList = pageList.getRecords();
        Long pages = pageList.getPages();
        return new PageResult<SchemeEmployee>(pageList.getTotal(),pages.intValue(),currentList);
    }

    /**
     * 根据 schemeEmployee的id 查询客户的详细参课情况
     * @param id
     * @return
     */
    @Override
    public List<SchemeResult> getSchemeDetails(String id) {
        QueryWrapper<SchemeResult> schemeResultQueryWrapper = new QueryWrapper<>();
        schemeResultQueryWrapper.eq("scheme_employee_id",id);
        List<SchemeResult> resultList = schemeResultMapper.selectList(schemeResultQueryWrapper);
        return resultList;
    }
}
